// pages/ makeMoney/index.js
const userUtils = require("../../utils/user");
const common = require('../../utils/common');
var pageUtils = require("../../utils/pageOptions.js");
const {
  getCmsList
} = require('../../services/cms')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '', //用来请求cms活动
    userInfo: null,
    cmsInfo: null,
    contentStatus: -1,
    containerPaddingBottom: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //业务埋点
    //0.禁止转发
    wx.hideShareMenu();
    //1.初始化数据
    this.initData(options);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //如果是分享进来的，并且没有选择角色，进入角色选择页面
    if (pageUtils.pageOptionsInfo()) {
      //2.relauch
      wx.reLaunch({
        url: '/pages/login/index'
      });
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // 业务埋点
  },

  //初始化options数据
  initUnShareOptionsData: function (options) {
    //userInfo
    const tempUserInfo = userUtils.getUserInfo();
    //productId
    var tmpId = options.id ? options.id : '';
    if (!tmpId) {
      common.showToast("活动不存在！");
      return
    }

    //set data
    this.setData({
      id: tmpId,
      userInfo: tempUserInfo
    });
  },

  //初始化数据
  initData: function (options) {
    let tmpId = options.id ? options.id : '';
    const userInfo = userUtils.getUserInfo();
    const isLogin = userInfo && userInfo.login;

    //没有登录进入登录页面
    if (!isLogin) {
      //1.save param
      var obj = {
        url: '../makeMoney/index',
        param: '?id=' + tmpId
      }
      pageUtils.savePageOptionsInfo(obj);
      //2.relauch
      //*********跳转放在onshow
      return;
    } else {
      //有角色，展示页面
      this.initUnShareOptionsData(options);
      //请求数据
      this.requestToGetCmsList(this.data.id);
    }
  },

  //获取cms详情
  requestToGetCmsList: function (cmsId) {
    let data = {
      id: cmsId
    };
    getCmsList(data).then(res => {
      this.handleCmslistData(res);
    }).catch(err => {
      common.log(err);
    });
  },

  //处理返回的商品列表数据
  handleCmslistData: function (res) {
    if (!res.data) {
      common.showToast("活动不存在！");
      return
    }
    var tmpInfo = res.data;
    //topPading 
    var tmpPaddintBottom = 0;
    if (tmpInfo.components && tmpInfo.components.length > 0) {
      var lastComponent = tmpInfo.components[tmpInfo.components.length - 1];
      //商品
      if (lastComponent.componentType == 3) {
        tmpPaddintBottom = 20;
      }
    }

    //1.set data
    this.setData({
      cmsInfo: tmpInfo,
      containerPaddingBottom: tmpPaddintBottom,
      contentStatus: 1
    });
    //2.设置标题
    wx.setNavigationBarTitle({
      title: tmpInfo.pageTitle
    });
    //3.开启转发
    wx.showShareMenu({});
  },

  // 点击配置的图片
  onImagesItem: function (e) {
    var componentIndex = e.currentTarget.dataset.cindex;
    var index = e.currentTarget.dataset.index;
    var componentItem = this.data.cmsInfo.components[componentIndex];
    var item = componentItem.images[index];

    //跳转
    if (item.paramType == 0) return;
    //商品详情
    if (item.paramType == 2) {
      return;
    }
    //其他
    if (item.paramType == 1) {
      //去掉头部的pages/
      var tmpLink = '../' + item.url;
      wx.navigateTo({
        url: tmpLink
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    //埋点
    return this.shareAppMessage();
  },

  //获取分享的AppMessage
  shareAppMessage: function () {
    return {
      title: '赚钱攻略',
      path: '/pages/makeMoney/makeMoney' + '?id=' + this.data.id,
      imageUrl: this.data.cmsInfo.mainImage
    }
  }
})
